package com.spzx.JUC2.test5;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Printer {
    ReentrantLock  reentrantLock = new ReentrantLock();
    Condition condition= reentrantLock.newCondition();

    public void print() throws InterruptedException {
        reentrantLock.lock();
        for(int x = 1 ; x <= 52 ; x++) {
            System.out.print(x);
            if(x % 2 == 0) {
                try {
                    condition.await();   // 等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            condition.signalAll();
    }
        reentrantLock.unlock();
    }

    // 打印字母
    public void printChar() {

        // 获取锁
        reentrantLock.lock();
        //判断
        for(char x = 'A' ; x <= 'Z' ; x++) {
            System.out.print(x);
            condition.signalAll();          // 打印完毕以后进行唤醒
            try {
                condition.await();          // 唤醒完毕以后进行等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 释放锁
        reentrantLock.unlock();

    }
}
